//
//  XMSharePlainController.m
//  XiaoMiao
//
//  Created by 23 on 2017/11/9.
//  Copyright © 2017年 smallyou. All rights reserved.
//

#import "XMSharePlainController.h"
#import "XMSharePlainView.h"
#import "OpenShareHeader.h"

@interface XMSharePlainController () <XMSharePlainViewDelegate>

/**分享界面*/
@property(nonatomic,weak) XMSharePlainView *shareView;

@end

@implementation XMSharePlainController

- (instancetype)init
{
    if (self = [super init]) {
        
        self.modalPresentationStyle = UIModalPresentationCustom;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //设置UI
    [self setupUI];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    
    [self showSharePlain];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismissSharePlain];

}


- (void)dismissSharePlain
{
    [self.shareView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(180);
    }];
    
    
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];

}

- (void)showSharePlain
{
    [self.shareView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom);
    }];
    
    
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    }];
}


#pragma mark - 设置UI
- (void)setupUI
{
    self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    
    XMSharePlainView *shareView = [XMSharePlainView sharePlainView];
    shareView.delegate = self;
    [self.view addSubview:shareView];
    self.shareView = shareView;
    
    
    [shareView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(180);
        make.height.mas_equalTo(180);
    }];
}


#pragma mark - XMSharePlainDelegate
- (void)sharePlain:(XMSharePlainView *)shareView didSelectedShareType:(XMShareType)type
{
    if (!self.message) {
        return;
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
       
        //创建分享消息
        OSMessage *msg = [[OSMessage alloc]init];
        msg.title =  self.message[@"title"];
        msg.link = self.message[@"link"];
        msg.image = self.message[@"image"];
        msg.thumbnail = self.message[@"thumbnail"];
        msg.desc = self.message[@"desc"];
        
        switch (type) {
            case XMShareTypeWechat:
            {
                if (![OpenShare isWeixinInstalled]) {
//                    [XMBannerView showErrorBannerWithTitle:@"未安装微信"];
                    return;
                }
                
                [OpenShare shareToWeixinSession:msg Success:^(OSMessage *message) {
//                    [XMBannerView showSuccessBannerWithTitle:@"成功分享到微信好友"];
                } Fail:^(OSMessage *message, NSError *error) {
//                    [XMBannerView showErrorBannerWithTitle:@"分享给微信好友失败"];
                }];
                
            }
                break;
            case XMShareTypeWechatFriends:
            {
                if (![OpenShare isWeixinInstalled]) {
//                    [XMBannerView showErrorBannerWithTitle:@"未安装微信"];
                    return;
                }
                
                [OpenShare shareToWeixinTimeline:msg Success:^(OSMessage *message) {
//                    [XMBannerView showSuccessBannerWithTitle:@"成功分享到微信朋友圈"];
                } Fail:^(OSMessage *message, NSError *error) {
//                    [XMBannerView showErrorBannerWithTitle:@"分享到微信朋友圈失败"];
                }];
            }
                break;
            case XMShareTypeQQ:
            {
                if (![OpenShare isQQInstalled]) {
//                    [XMBannerView showErrorBannerWithTitle:@"未安装QQ"];
                    return;
                }
                
                [OpenShare shareToQQFriends:msg Success:^(OSMessage *message) {
//                    [XMBannerView showSuccessBannerWithTitle:@"成功分享到QQ"];
                } Fail:^(OSMessage *message, NSError *error) {
//                    [XMBannerView showErrorBannerWithTitle:@"分享给QQ好友失败"];
                }];
            }
                break;
            case XMShareTypeWeibo:
            {
                if (![OpenShare isWeiboInstalled]) {
//                    [XMBannerView showErrorBannerWithTitle:@"未安装微博"];
                    return;
                }
                
                [OpenShare shareToWeibo:msg Success:^(OSMessage *message) {
//                    [XMBannerView showSuccessBannerWithTitle:@"成功分享到微博"];
                } Fail:^(OSMessage *message, NSError *error) {
//                    [XMBannerView showErrorBannerWithTitle:@"分享到微博失败"];
                }];
                
            }
                break;
                
            default:
                break;
        }
        
        
    }];
}

- (void)sharePlainDidCancel:(XMSharePlainView *)shareView
{
    [self dismissSharePlain];
}


@end
